package com.whfc.iot.mach.codec;

import com.whfc.common.util.ByteUtil;
import com.whfc.iot.mach.constant.MsgConst;
import com.whfc.iot.mach.entity.*;
import io.netty.buffer.ByteBuf;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/17 9:32
 */
public class MachMsgDataCodec0x87 implements MachMsgDataCodec<MachMsg0x87> {

    @Override
    public void decode(ByteBuf buf, MachMsg0x87 msg) {
        List<MachParamItem> paramList = new ArrayList<>();
        int paramNum = buf.readUnsignedByte();
        for (int i = 0; i < paramNum; i++) {
            int paramType = buf.readUnsignedByte();
            //报警参数
            if (MsgConst.PARAM_TYPE_ROTARY == paramType) {
                int front = buf.readUnsignedByte();
                int back = buf.readUnsignedByte();
                int left = buf.readUnsignedByte();
                int right = buf.readUnsignedByte();
                MachParamRotary param = new MachParamRotary();
                param.setLeft(left);
                param.setRight(right);
                param.setFront(front);
                param.setBack(back);
                paramList.add(param);
            }
            //lora appEui
            else if (MsgConst.PARAM_TYPE_LORA_APP_EUI == paramType) {
                int[] bytes = new int[8];
                for (int j = 0; j < bytes.length; j++) {
                    bytes[j] = buf.readUnsignedByte();
                }
                String appEui = ByteUtil.transform(bytes);
                MachParamLoraAppEui param = new MachParamLoraAppEui();
                param.setAppEui(appEui);
                paramList.add(param);
            }
            //lora appKey
            else if (MsgConst.PARAM_TYPE_LORA_APP_KEY == paramType) {
                int[] bytes = new int[16];
                for (int j = 0; j < bytes.length; j++) {
                    bytes[j] = buf.readUnsignedByte();
                }
                String appKey = ByteUtil.transform(bytes);
                MachParamLoraAppKey param = new MachParamLoraAppKey();
                param.setAppKey(appKey);
                paramList.add(param);
            }
            //阈值
            else if (MsgConst.PARAM_TYPE_THRESHOLD == paramType) {
                int count = buf.readUnsignedByte();
                List<MachThresHold> thresHoldList = new ArrayList<>(count);
                for (int j = 0; j < count; j++) {
                    int type = buf.readUnsignedByte();
                    float value = buf.readFloat();
                    MachThresHold thresHold = new MachThresHold(type, value);
                    thresHoldList.add(thresHold);
                }
                MachParamThreshold param = new MachParamThreshold();
                param.setCount(count);
                param.setThresHoldList(thresHoldList);
                paramList.add(param);
            }
        }
        msg.setParamNum(paramNum);
        msg.setParamList(paramList);
    }

    @Override
    public void encode(ByteBuf buf, MachMsg0x87 msg) {
        List<MachParamItem> paramList = msg.getParamList();
        int paramNum = paramList.size();
        buf.writeByte(paramNum);
        if (paramList != null && paramList.size() > 0) {
            for (MachParamItem item : paramList) {
                int paramType = item.paramType();
                buf.writeByte(paramType);
                //报警参数
                if (MsgConst.PARAM_TYPE_ROTARY == paramType) {
                    MachParamRotary param = (MachParamRotary) item;
                    buf.writeByte(param.getFront());
                    buf.writeByte(param.getBack());
                    buf.writeByte(param.getLeft());
                    buf.writeByte(param.getRight());
                }
                //lora appEui
                else if (MsgConst.PARAM_TYPE_LORA_APP_EUI == paramType) {
                    MachParamLoraAppEui param = (MachParamLoraAppEui) item;
                    int[] bytes = ByteUtil.transform(param.getAppEui());
                    for (int data : bytes) {
                        buf.writeByte(data);
                    }
                }
                //lora appKey
                else if (MsgConst.PARAM_TYPE_LORA_APP_KEY == paramType) {
                    MachParamLoraAppKey param = (MachParamLoraAppKey) item;
                    int[] bytes = ByteUtil.transform(param.getAppKey());
                    for (int data : bytes) {
                        buf.writeByte(data);
                    }
                }
                //阈值
                else if (MsgConst.PARAM_TYPE_THRESHOLD == paramType) {
                    MachParamThreshold param = (MachParamThreshold) item;
                    List<MachThresHold> thresHoldList = param.getThresHoldList();
                    int count = thresHoldList != null ? thresHoldList.size() : 0;
                    buf.writeByte(count);
                    if (count > 0) {
                        for (MachThresHold thresHold : thresHoldList) {
                            buf.writeByte(thresHold.getType());
                            buf.writeFloat(thresHold.getValue());
                        }
                    }
                }
            }
        }
    }
}
